package com.relax.mybatis.encrypt.algorithm;

import java.security.MessageDigest;

/**
 * @author relax
 * @date 2022/07/05
 * @desc MD5加密
 */
public class MD5 {

    public static byte[] encode(byte[] data) throws Exception {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(data);
        return messageDigest.digest();
    }

    public static String encodeToBase64(String data) throws Exception {
        return Base64.encodeToString(encode(data.getBytes()));
    }

    public static String encodeToHex(String data) throws Exception {
        return Hex.encodeToString(encode(data.getBytes()));
    }

    public static String encode16ToBase64(String data) throws Exception {
        return encodeToBase64(data).substring(8, 24);
    }

    public static String encode16ToHex(String data) throws Exception {
        return encodeToHex(data).substring(8, 24);
    }

}
